package org.example.demo3.common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.Serializable;

//拦截器
@Configuration
public class Interceptor implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new Intercept()).
                addPathPatterns("/**").
                excludePathPatterns("/css/**").
                excludePathPatterns("/img/**").
                excludePathPatterns("/js/**").
                excludePathPatterns("/reg.html").
                excludePathPatterns("/blog_list.html").
                excludePathPatterns("/user/reg").
                excludePathPatterns("/user/login").
                excludePathPatterns("/editor.md/**").
                excludePathPatterns("/article/allpage").
                excludePathPatterns("/article/this_page_content").
                excludePathPatterns("/article/getresponse").
                excludePathPatterns("/article/getreview").
                excludePathPatterns("/blog_list_content.html").
                excludePathPatterns("/article/getarticlecontent").
                excludePathPatterns("/article/rcount").
                excludePathPatterns("/blog_content.html").
                excludePathPatterns("/article/get_response_res").
                excludePathPatterns("/article/setresponse_res").
                excludePathPatterns("/login.html");
    }
}
@Configuration
class Intercept implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession(false);
        if (session==null){
            response.sendRedirect("/login.html");
//            return false;
        }
        return true;
    }
}